# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/6/4
# @File   : EC09_WhiteUser.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:是否为企业征信白户
code:EC09_WhiteUser
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ
type:常用指标
dataSourceType:在线指标
description:
eof'''

import sys
import re


reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

# 由于plutus平台out指标 只有当返回缺失值时 才触发  故当为白户时 返回缺失值
def EC09_WhiteUser():
    try:
        ECRSummaryDesc = getTable('ECRSummaryDesc')
        # 表异常为白户
        if ECRSummaryDesc is None or ECRSummaryDesc == [{}]:
            return u'缺失值'
        else:
            pattern = u'[\s+\.\!\/_,$%^*(+\"\')]+|[+——()?【】“”！，。？、~@#￥%……&*（） ]+'
            pattern1 = u'信息主体未与金融机构发生过信贷关系'
            # pattern2 = u'共在([0-9]+)家金融机构办理过信贷业务'

            descLine = ECRSummaryDesc[0].get('BriefDesc')
            if descLine not in null_type_list:
                # 去除符号
                descLine = re.sub(pattern,u'', descLine)

                if pattern1 in descLine:
                    return u'缺失值'
                else:
                    return 0
            else:
                return u'缺失值'
    except:
        return u'缺失值'

result = EC09_WhiteUser()